<?php
namespace app\cms\http\ueditor;

use Inphp\Core\Context;

class index
{
    public function index(){
        //header('Access-Control-Allow-Origin: http://www.baidu.com'); //设置http://www.baidu.com允许跨域访问
        //header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With'); //设置允许的跨域header
        //date_default_timezone_set("Asia/chongqing");
        //error_reporting(E_ERROR);
        //header("Content-Type: text/html; charset=utf-8");

        $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents(__DIR__."/config.json")), true);
        $action = GET('action');

        switch ($action) {
            case 'config':
                $result =  $CONFIG;
                break;

            /* 上传图片 */
            case 'uploadimage':
                /* 上传涂鸦 */
            case 'uploadscrawl':
                /* 上传视频 */
            case 'uploadvideo':
                /* 上传文件 */
            case 'uploadfile':
                $result = (new action_upload())->index();
                break;
            /* 列出文件 */
            /* 列出图片 */
            case 'listfile':
            case 'listimage':
                $result = (new action_list())->index();
                break;
            /* 抓取远程文件 */
            case 'catchimage':
                $result = (new action_crawler())->index();
                break;

            default:
                $result = array(
                    'state'=> '请求地址出错'
                );
                break;
        }
        $result = json_encode($result, JSON_UNESCAPED_UNICODE);
        $response = Context::getResponse();
        /* 输出结果 */
        $callback = GET("callback");
        if (!empty($callback)) {
            if (preg_match("/^[\w_]+$/", $callback)) {
                $response->withHTML(htmlspecialchars($callback) . '(' . $result . ')')->end();
            } else {
                $response->withHTML(json_encode(['state'=> 'callback参数不合法'], JSON_UNESCAPED_UNICODE))->end();
            }
        } else {
            $response->withHTML($result)->end();
        }
        return $response;
    }
}